package org.aeon.client.view.components.sessionPanel;

import org.aeon.constant.GroupNotificationType;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/21 12:51
 * @description:
 */
public class GroupNotificationsItem extends JPanel {
    public GroupNotificationsItem(GroupNotificationType type, String username, String nickname, String time, String groupName) {
        this.setBackground(new Color(0x93A8A8A8, true));
        this.setBorder(createRoundedBorder());
        this.setLayout(new GridBagLayout());
        this.setPreferredSize(new Dimension(600, 60));
        this.setOpaque(false);


        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.LINE_START;
        constraints.insets = new Insets(5, 5, 0, 5); // 设置组件之间的间距

        final JLabel userLabel = new JLabel(nickname + "(" + username + ")");
        constraints.gridx = 0;
        constraints.gridy = 0;
        this.add(userLabel, constraints);

        final JLabel timeLabel = new JLabel(time);
        constraints.gridx = 1;
        this.add(timeLabel, constraints);

        final JLabel notificationLabel = new JLabel();
        switch (type) {
            case INVITE_TO_JOIN -> notificationLabel.setText("邀请你加入");
            case KICK_OUT -> notificationLabel.setText("被踢出");
            case DISSOLUTION -> notificationLabel.setText("已解散");
        }
        constraints.gridx = 0;
        constraints.gridy = 1;
        this.add(notificationLabel, constraints);

        final JLabel attachmentLabel = new JLabel(groupName);
        // 设置文本左对齐
        userLabel.setHorizontalAlignment(JLabel.LEFT);
        final Dimension dimension = new Dimension(350, 20);
        attachmentLabel.setPreferredSize(dimension);
        attachmentLabel.setMaximumSize(dimension);
        attachmentLabel.setMinimumSize(dimension);
        constraints.gridx = 1;
        constraints.gridy = 1;
        this.add(attachmentLabel, constraints);

        // 在标签和按钮之间添加水平空白
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.ipadx = 250;
        constraints.gridwidth = 1;
        constraints.gridheight = 2;
        this.add(Box.createHorizontalStrut(60), constraints);

        final JButton operationButton = new JButton("同意");
        operationButton.setContentAreaFilled(false);
        constraints.gridx = 3;
        constraints.gridy = 0;
        constraints.ipadx = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 2;
        this.add(operationButton, constraints);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(getBackground());
        g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
        g2d.dispose();
    }

    private Border createRoundedBorder() {
        return BorderFactory.createEmptyBorder(10, 10, 10, 10);
    }
}
